﻿#include "pagenavigator.h"

#include <QtGui/QIntValidator>
#include <QtGui/QKeyEvent>

PageNavigator::PageNavigator(int pageSize, QWidget *parent)
	: QWidget(parent)
	//, ui(new Ui::PageNavigator)
{
    ui.setupUi(this);
    setPageSize(pageSize);
    initialize();

	m_maxPage = 0;
    setMaxPage(1);

	QString qss = QString(".QLabel[page=\"true\"] { padding: 6px; }")
		+ QString(".QLabel[currentPage=\"true\"] { color: rgb(190, 0, 0);}")
		+ QString(".QLabel[page=\"true\"]:hover { color: white; border-radius: 4px; background-color: qlineargradient(spread:reflect, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(53, 121, 238, 255), stop:1 rgba(0, 202, 237, 255));}");
	this->setStyleSheet(qss);
}

PageNavigator::~PageNavigator()
{
    //delete ui;

}

// 初始化
void PageNavigator::initialize()
{

    ui.nextPageLabel->setProperty("page", "true");
    ui.previousPageLabel->setProperty("page", "true");
    ui.nextPageLabel->installEventFilter(this);
    ui.previousPageLabel->installEventFilter(this);

    ui.firstPageLabel->setProperty("page", "true");
    ui.lastPageLabel->setProperty("page", "true");
    ui.gotoPageLabel->setProperty("page", "true");
    ui.firstPageLabel->installEventFilter(this);
    ui.lastPageLabel->installEventFilter(this);
    ui.gotoPageLabel->installEventFilter(this);

    ui.curpage->setReadOnly(true);
    ui.gotopage->setText("1");
}

//事件响应
bool PageNavigator::eventFilter(QObject *watched, QEvent *e)
{
    if (e->type() == QEvent::MouseButtonRelease)
	{
        int page = getCurrentPage();

        //首页
        if (watched == ui.firstPageLabel)
        {
            page = 1;
        }

        //上一页
        if (watched == ui.previousPageLabel)
		{            
            if(getCurrentPage() > 1)
            {
                page = getCurrentPage() - 1;
            }
		}

        //下一页
        if (watched == ui.nextPageLabel)
		{            
            if(getCurrentPage() < getMaxPage())
            {
                page = getCurrentPage() + 1;
            }
		}

        //尾页
        if (watched == ui.lastPageLabel)
        {
            page = getMaxPage();
        }

        //跳转
        if (watched == ui.gotoPageLabel)
        {
            int tmppage = ui.gotopage->text().toUInt();
            if(tmppage < 1)
            {
                tmppage = 1;
            }
            else if(tmppage > getMaxPage())
            {
                tmppage = getMaxPage();
            }

            page = tmppage;
            ui.gotopage->setText(QString("%1").arg(page));
        }

        //设置页码
        if (page != getCurrentPage())
		{			
            setCurrentPage(page, true);
            return true;
        }

    }

    return QWidget::eventFilter(watched, e);
}

int PageNavigator::getPageNum() const
{
    return m_pageSize;
}

int PageNavigator::getMaxPage() const
{
    return m_maxPage;
}

int PageNavigator::getCurrentPage() const
{
    return m_currentPage;
}

//设置总条数
void PageNavigator::setTotalNum(int totalnum)
{
    ui.pagetotal->setText(QString::fromLocal8Bit("每页 %1 条，共 %2 条").arg(m_pageSize).arg(totalnum));

    m_maxPage = totalnum / m_pageSize;
    if(totalnum % m_pageSize != 0)
    {
       m_maxPage += 1;
    }

    updatePageLabels();
}

void PageNavigator::setMaxPage(int page)
{
    page = qMax(page, 1);

    //限制器
    ui.gotopage->setValidator(new QIntValidator(1, page, this));

    if (m_maxPage != page)
	{
        m_maxPage = page;
        m_currentPage = 1;
        updatePageLabels();
    }
}

void PageNavigator::setCurrentPage(int page, bool signalEmitted)
{
    page = qMax(page, 1);
    page = qMin(page, m_maxPage);

    if (page != m_currentPage)
	{
        m_currentPage = page;
        updatePageLabels();

        if (signalEmitted)
		{
            emit currentPageChanged(page);
        }
    }
}

void PageNavigator::setPageSize(int pageSize)
{    
    pageSize = qMax(pageSize, 3);

    m_pageSize = pageSize;
}





void PageNavigator::updatePageLabels()
{
    // 以下情况为maxPageNumber大于blockSize * 3, 所有的页码label都要显示
    // c 为 currentPage
    // n 为 block size
    // m 为 maxPage

    int c = m_currentPage;
    int n = m_pageSize;
    int m = m_maxPage;

    ui.curpage->setText(QString("%1/%2").arg(c).arg(m));

}
